package base.多线程.同步代码.同步方法;

public class MM_Runnable implements Runnable{
    int ticket = 0;
    boolean flag = true;
    
    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (flag) runnable();
            else break;
        }
    }

    private synchronized void runnable(){
        if (ticket == 100){
            System.out.println("当前票已经售罄~");
            flag = false;
        }else {
            ticket++;
            System.out.println(Thread.currentThread().getName()+"正在销售第"+ticket+"张票;");
        }
    }

}
